#!/usr/bin/env node

//import { argv } from 'process';

const arg = process.argv;
//  stdout = process.stdout;

if(arg.length === 3){
  let a = arg[2].split(',');
  if(a.length <2){
    console.error('命令行参数少于两个');
    process.exit(1);
  }else{
    deal(a);
  }
}if(arg.length == 2){
  console.error('请输入参数！');
  process.exit(2);
}else{
  deal(arg.slice(2));
}

function deal(arg){
  let sum = 0;
  for(let n of arg){
    if(isNaN(n)){
      console.error('请输入数字！');
      process.exit(3);
    }
    if(n > 1 || n < 0){
      console.error('请输入0到1的数字');
      process.exit(4);
    }
    sum = sum + Number(n);
    
  }
  if(sum > 1){
    console.error('概率空间不分布，概率和大于1');
    process.exit(5);
  }
  if(sum < 1){
    console.error('概率空间不封闭，概率和小于1');
    process.exit(6);
  }
  let p = [];
  for(let j = 0;j < arg.length; j++){
    p[j] = Number(arg[j]);
  }
  let h = 0;
  for(let m = 0; m < arg.length; m++){
    h = h-p[m]*Math.log(p[m] , 2);
  }
  h = h.toFixed(3);
  
  console.log(`p = ${p} `);
  console.log(`p(h) = ${h} bit`);
}
  

  



//console.log(a);






